/**
 * 
 */
package tarena.netctoss.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import tarena.netctoss.dao.BusinessCondition;
import tarena.netctoss.dao.BusinessDao;
import tarena.netctoss.dao.Pagination;
import tarena.netctoss.po.Business;
import tarena.netctoss.po.User;

/**
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-3-9
 */

public class BusinessDaoImpl implements BusinessDao {

	private HibernateTemplate hibernateTemplate;

	/**
	 * 依赖注入
	 * 
	 * @param hibernateTemplate
	 */
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.BusinessDao#delete(tarena.netctoss.po.Business)
	 */
	@Override
	public void delete(Business business) {
		hibernateTemplate.delete(business);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.BusinessDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Business> findAll() {
		return hibernateTemplate.find("from Business order by loginname");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * tarena.netctoss.dao.BusinessDao#findAll(tarena.netctoss.dao.BusinessCondition
	 * , tarena.netctoss.dao.Pagination)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Business> findAll(final BusinessCondition bc,
			final Pagination page) {
		return hibernateTemplate.executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				@SuppressWarnings("unused")
				Criteria c = getCriteria(session, bc);
				return null;
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * tarena.netctoss.dao.BusinessDao#findAll(tarena.netctoss.dao.BusinessCondition
	 * )
	 */
	@Override
	public List<Business> findAll(BusinessCondition bc) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.BusinessDao#findById(java.lang.Integer)
	 */
	@Override
	public Business findById(Integer id) {
		return (Business) hibernateTemplate.get(Business.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.BusinessDao#findByUserId(java.lang.Integer)
	 */
	@Override
	public Business findByUserId(Integer userId) {
		return (Business) hibernateTemplate.get(Business.class, userId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * tarena.netctoss.dao.BusinessDao#loadMaxPage(tarena.netctoss.dao.Pagination
	 * , tarena.netctoss.dao.BusinessCondition)
	 */
	@Override
	public void loadMaxPage(final Pagination page, final BusinessCondition bc) {
		hibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria c = getCriteria(session, bc);
				// 添加多行函数查询
				// select count(*) from Pricing where name like ....
				c.setProjection(Projections.rowCount());
				Object o = c.uniqueResult();
				// hibernate 低版本count(*)返回 Integer
				// hibernate 高版本count(*)返回 Long
				int rowCount = 0;
				if (o instanceof Long) {
					rowCount = ((Long) o).intValue();
				} else {
					rowCount = ((Integer) o).intValue();
				}
				int maxPage = (rowCount + page.getPageSize() - 1)
						/ page.getPageSize();
				page.setMaxPage(maxPage);
				return null;

			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.BusinessDao#save(tarena.netctoss.po.Business)
	 */
	@Override
	public void save(Business business) {

		hibernateTemplate.save(business);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.BusinessDao#update(tarena.netctoss.po.Business)
	 */
	@Override
	public void update(Business business) {
		hibernateTemplate.update(business);
	}

	/**
	 * 获得
	 * 
	 * @param session
	 * @param bc
	 * @return
	 */
	private Criteria getCriteria(Session session, BusinessCondition bc) {
		Criteria c = session.createCriteria(Business.class);
		// 用户名,用户ID
		if (null != bc.getUserName() && !"".equals(bc.getUserName())) {
			User u = (User) session.get(Business.class, bc.getUserName());
			c.add(Restrictions.eq("userid", u.getId()));
		}
		// 业务登录名
		if (null != bc.getLoginname() && !"".equals(bc.getLoginname())) {
			c.add(Restrictions.eq("loginname", bc.getLoginname()));
		}
		// 实验室IP
		if (null != bc.getLabIp() && !"".equals(bc.getLabIp())) {
			c.add(Restrictions.eq("labip", bc.getLabIp()));
		}
		// 状态:
		if (bc.getStatus() >= 0) {
			c.add(Restrictions.eq("status", bc.getStatus()));
		}
		return c;
	}
}
